<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
      <el-form-item label="用户ID" prop="userId">
        <el-input v-model="dataForm.userId"  required  placeholder="用户ID"></el-input>
      </el-form-item>
      <el-form-item label="订单号" prop="orderSn">
        <el-input v-model="dataForm.orderSn"  required  placeholder="订单号"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="userUsername">
        <el-input v-model="dataForm.userUsername"  required  placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="订单总额（元）" prop="totalAmount">
        <el-input v-model="dataForm.totalAmount"  required  placeholder="订单总额"></el-input>
      </el-form-item>
      <el-form-item label="应付总额（元）" prop="payAmount">
        <el-input v-model="dataForm.payAmount"  required  placeholder="应付总额"></el-input>
      </el-form-item>
      <el-form-item label="运费金额（元）" prop="freightAmount">
        <el-input v-model="dataForm.freightAmount"  required  placeholder="运费金额"></el-input>
      </el-form-item>
      <el-form-item label="促销优化金额（元）" prop="promotionAmount">
        <el-input v-model="dataForm.promotionAmount"  required  placeholder="促销优化金额"></el-input>
      </el-form-item>
            <el-form-item 
        label="支付方式"
        prop="payType"
      >
        <template>
          <el-radio-group v-model="dataForm.payType">
          <el-radio :label="1">支付宝</el-radio>
          <el-radio :label="2">微信</el-radio>
          <el-radio :label="3">银联</el-radio>
          <el-radio :label="4">货到付款</el-radio>
          </el-radio-group>
        </template>
      </el-form-item>
            <el-form-item 
        label="订单状态"
        prop="status"
      >
        <template>
          <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">待付款</el-radio>
          <el-radio :label="1">待发货</el-radio>
          <el-radio :label="2">已发货</el-radio>
          <el-radio :label="3">已完成</el-radio>
          <el-radio :label="4">已关闭</el-radio>
          <el-radio :label="5">无效订单</el-radio>
          </el-radio-group>
        </template>
      </el-form-item>
      <el-form-item label="物流公司(配送方式)" prop="deliveryCompany">
        <el-input v-model="dataForm.deliveryCompany"  required  placeholder="物流公司(配送方式)"></el-input>
      </el-form-item>
      <el-form-item label="物流单号" prop="deliverySn">
        <el-input v-model="dataForm.deliverySn"  required  placeholder="物流单号"></el-input>
      </el-form-item>
      <el-form-item label="自动确认时间（天）" prop="autoConfirmDay">
        <el-input v-model="dataForm.autoConfirmDay"  required  placeholder="自动确认时间（天）"></el-input>
      </el-form-item>
      <el-form-item label="收货人姓名" prop="receiverName">
        <el-input v-model="dataForm.receiverName"  required  placeholder="收货人姓名"></el-input>
      </el-form-item>
      <el-form-item label="收货人电话" prop="receiverPhone">
        <el-input v-model="dataForm.receiverPhone"  required  placeholder="收货人电话"></el-input>
      </el-form-item>
      <el-form-item label="收货人邮编" prop="receiverPostCode">
        <el-input v-model="dataForm.receiverPostCode"  required  placeholder="收货人邮编"></el-input>
      </el-form-item>
      <el-form-item label="省份/直辖市" prop="receiverProvince">
        <el-input v-model="dataForm.receiverProvince"  required  placeholder="省份/直辖市"></el-input>
      </el-form-item>
      <el-form-item label="城市" prop="receiverCity">
        <el-input v-model="dataForm.receiverCity"  required  placeholder="城市"></el-input>
      </el-form-item>
      <el-form-item label="区" prop="receiverRegion">
        <el-input v-model="dataForm.receiverRegion"  required  placeholder="区"></el-input>
      </el-form-item>
      <el-form-item label="详细地址" prop="receiverDetailAddress">
        <el-input v-model="dataForm.receiverDetailAddress"  required  placeholder="详细地址"></el-input>
      </el-form-item>
      <el-form-item label="订单备注" prop="note">
        <el-input v-model="dataForm.note"  required  placeholder="订单备注"></el-input>
      </el-form-item>
      <el-form-item 
        label="确认收货状态"
        prop="confirmStatus"
      >
        <template>
          <el-radio-group v-model="dataForm.confirmStatus">
          <el-radio :label="1">已确认</el-radio>
          <el-radio :label="0">未确认</el-radio>
          </el-radio-group>
        </template>
      </el-form-item>
      <el-form-item 
        label="删除状态"
        prop="deleteStatus"
      >
        <template>
          <el-radio-group v-model="dataForm.deleteStatus">
          <el-radio :label="1">已删除</el-radio>
          <el-radio :label="0">未删除</el-radio>
          </el-radio-group>
        </template>
      </el-form-item>
      <el-form-item label="支付时间" prop="paymentTime">
        <el-input v-model="dataForm.paymentTime"  required  placeholder="支付时间"></el-input>
      </el-form-item>
      <el-form-item label="发货时间" prop="deliveryTime">
        <el-input v-model="dataForm.deliveryTime"  required  placeholder="发货时间"></el-input>
      </el-form-item>
      <el-form-item label="确认收货时间" prop="receiveTime">
        <el-input v-model="dataForm.receiveTime"  required  placeholder="确认收货时间"></el-input>
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="visible = false">取消</el-button>
      <el-button
        type="primary"
        @click="dataFormSubmit()"
      >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        userId: '',
        userUsername: '',
        orderSn: '',
        createTime: '',
        newTime: '',
        totalAmount: '',
        payAmount: '',
        freightAmount: '',
        promotionAmount: '',
        payType: '',
        status: '',
        deliveryCompany: '',
        deliverySn: '',
        autoConfirmDay: '',
        receiverName: '',
        receiverPhone: '',
        receiverPostCode: '',
        receiverProvince: '',
        receiverCity: '',
        receiverRegion: '',
        receiverDetailAddress: '',
        note: '',
        confirmStatus: 0,
        deleteStatus: 0,
        paymentTime: '',
        deliveryTime: '',
        receiveTime: '',
        commentTime: '',
        modifyTime: ''
      },
      dataRule: {
        userId: [{ required: true, message: '用户id不能为空', trigger: 'blur' }],
        userUsername: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        orderSn: [{ required: true, message: '订单号不能为空', trigger: 'blur' }],
        totalAmount: [{ required: true, message: '订单总额不能为空', trigger: 'blur' }],
        payAmount: [{ required: true, message: '应付总额不能为空', trigger: 'blur' }],
        freightAmount: [{ required: true, message: '运费金额不能为空', trigger: 'blur' }],
        promotionAmount: [{ required: true, message: '促销优化金额不能为空', trigger: 'blur' }],
        payType: [{required: true, message: '支付方式【1->支付宝；2->微信；3->银联； 4->货到付款；】不能为空', trigger: 'blur'}],
        status: [{required: true, message: '订单状态【0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单】不能为空', trigger: 'blur'}],
        deliveryCompany: [{ required: true, message: '物流公司(配送方式)不能为空', trigger: 'blur' }],
        deliverySn: [{ required: true, message: '物流单号不能为空', trigger: 'blur' }],
        autoConfirmDay: [{ required: true, message: '自动确认时间（天）不能为空', trigger: 'blur' }],
        receiverName: [{ required: true, message: '收货人姓名不能为空', trigger: 'blur' }],
        receiverPhone: [{ required: true, message: '收货人电话不能为空', trigger: 'blur' }],
        receiverPostCode: [{ required: true, message: '收货人邮编不能为空', trigger: 'blur' }],
        receiverProvince: [{ required: true, message: '省份/直辖市不能为空', trigger: 'blur' }],
        receiverCity: [{ required: true, message: '城市不能为空', trigger: 'blur' }],
        receiverRegion: [{ required: true, message: '区不能为空', trigger: 'blur' }],
        receiverDetailAddress: [{ required: true, message: '详细地址不能为空', trigger: 'blur' }],
        note: [{ required: true, message: '订单备注不能为空', trigger: 'blur' }],
        confirmStatus: [{required: true, message: '确认收货状态【0->未确认；1->已确认】不能为空', trigger: 'blur'}],
        deleteStatus: [{required: true, message: '删除状态【0->未删除；1->已删除】不能为空', trigger: 'blur'}],
        paymentTime: [{ required: true, message: '支付时间不能为空', trigger: 'blur' }],
        deliveryTime: [{ required: true, message: '发货时间不能为空', trigger: 'blur' }],
        receiveTime: [{ required: true, message: '确认收货时间不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // getCurrentTime () {
    //   this.newTime = new Date()
    //   console.log(this.newTime)
    // },
    init (id) {
      this.dataForm.id = id || 0
      this.$http({
        url: this.$http.adornUrl('/sys/role/list'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        this.roleList = data.page.list
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      }).then(() => {
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/order/order/info/${this.dataForm.id.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.userId = data.order.userId
              this.dataForm.userUsername = data.order.userUsername
              this.dataForm.orderSn = data.order.orderSn
              this.dataForm.createTime = data.order.createTime
              this.dataForm.totalAmount = data.order.totalAmount
              this.dataForm.payAmount = data.order.payAmount
              this.dataForm.freightAmount = data.order.freightAmount
              this.dataForm.promotionAmount = data.order.promotionAmount
              this.dataForm.payType = data.order.payType
              this.dataForm.status = data.order.status
              this.dataForm.deliveryCompany = data.order.deliveryCompany
              this.dataForm.deliverySn = data.order.deliverySn
              this.dataForm.autoConfirmDay = data.order.autoConfirmDay
              this.dataForm.receiverName = data.order.receiverName
              this.dataForm.receiverPhone = data.order.receiverPhone
              this.dataForm.receiverPostCode = data.order.receiverPostCode
              this.dataForm.receiverProvince = data.order.receiverProvince
              this.dataForm.receiverCity = data.order.receiverCity
              this.dataForm.receiverRegion = data.order.receiverRegion
              this.dataForm.receiverDetailAddress = data.order.receiverDetailAddress
              this.dataForm.note = data.order.note
              this.dataForm.confirmStatus = data.order.confirmStatus
              this.dataForm.deleteStatus = data.order.deleteStatus
              this.dataForm.paymentTime = data.order.paymentTime
              this.dataForm.deliveryTime = data.order.deliveryTime
              this.dataForm.receiveTime = data.order.receiveTime
              this.dataForm.commentTime = data.order.commentTime
              this.dataForm.modifyTime = data.order.modifyTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      // this.$options.methods.getCurrentTime()
      this.newTime = new Date()
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/user/user/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id.id || undefined,
              'userUsername': this.dataForm.userUsername,
              'orderSn': this.dataForm.orderSn,
              'totalAmount': this.dataForm.totalAmount,
              'userId': this.dataForm.userId,
              'payAmount': this.dataForm.payAmount,
              'freightAmount': this.dataForm.freightAmount,
              'promotionAmount': this.dataForm.promotionAmount,
              'payType': this.dataForm.payType,
              'deliveryCompany': this.dataForm.deliveryCompany,
              'status': this.dataForm.status,
              'deliverySn': this.dataForm.deliverySn,
              'autoConfirmDay': this.dataForm.autoConfirmDay,
              'receiverPhone': this.dataForm.receiverPhone,
              'receiverName': this.dataForm.receiverName,
              'receiverPostCode': this.dataForm.receiverPostCode,
              'receiverProvince': this.dataForm.receiverProvince,
              'receiverCity': this.dataForm.receiverCity,
              'receiverRegion': this.dataForm.receiverRegion,
              'receiverDetailAddress': this.dataForm.receiverDetailAddress,
              'note': this.dataForm.note,
              'confirmStatus': this.dataForm.confirmStatus,
              'deleteStatus': this.dataForm.deleteStatus,
              'paymentTime': this.dataForm.paymentTime,
              'deliveryTime': this.dataForm.deliveryTime,
              'receiveTime': this.dataForm.receiveTime,
              'commentTime': this.dataForm.commentTime,
              'modifyTime': this.newTime,
              'createTime': !this.dataForm.id ? this.newTime : this.dataForm.createTime
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
